/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.aptech.Controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 *
 * @author Hoang Quy Hong An
 */
public class avSearch extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        int realEstateStyleID;
        int realEstateTypeID;
        String city;
        int noOfBedrooms;
        String street;
        int noOfBathrooms;
        int price;
        String orientation;
        int deposit;
        int area;
        String strCommand = "";

        try {
            realEstateStyleID = Integer.parseInt(request.getParameter("realEstateStyleID"));
            realEstateTypeID = Integer.parseInt(request.getParameter("realEstateTypeID"));
            city = request.getParameter("city");
            street = request.getParameter("street");
            price = Integer.parseInt(request.getParameter("price"));
            orientation = request.getParameter("orientation");
            deposit = Integer.parseInt(request.getParameter("deposit"));
            area = Integer.parseInt(request.getParameter("area"));
            if (realEstateStyleID != 0) {
                strCommand += " AND tbl_RealEstate.realEstateStyleID = " + realEstateStyleID;
            }
            if (realEstateTypeID != 0) {
                strCommand += " AND tbl_RealEstate.realEstateTypeID = " + realEstateTypeID;
            }
            if (!city.isEmpty() && city != null) {
                strCommand += " AND  tbl_RealEstate.City LIKE '%" + city + "%'";
            }
            if (!request.getParameter("noOfBedrooms").isEmpty() && request.getParameter("noOfBedrooms") != null) {
                try {
                    noOfBedrooms = Integer.parseInt(request.getParameter("noOfBedrooms"));
                    strCommand += " AND tbl_RealEstate.NoOfBedrooms = " + noOfBedrooms;
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                }
            }
            if ( request.getParameter("noOfBathrooms") != null) {                
                try {
                    noOfBathrooms = Integer.parseInt(request.getParameter("noOfBathrooms"));
                    strCommand += " AND tbl_RealEstate.NoOfBedrooms = " + noOfBathrooms;
                } catch (NumberFormatException ex) {
                }
            }
            if (!street.isEmpty() && street != null) {
                strCommand += " AND tbl_RealEstate.Street LIKE '%" + street + "%'";
            }
            if (price != 0) {
                if (price == 1) {
                    strCommand +=" AND tbl_RealEstate.Price BETWEEN 50 AND 500";
                } else if (price == 2) {
                    strCommand +=" AND tbl_RealEstate.Price BETWEEN 500 AND 1000";
                } else if (price == 3) {
                    strCommand +=" AND tbl_RealEstate.Price BETWEEN 1000 AND 2000";
                } else if (price == 4) {
                    strCommand +=" AND tbl_RealEstate.Price BETWEEN 2000 AND 3000";
                } else if (price == 5) {
                    strCommand +=" AND tbl_RealEstate.Price BETWEEN 3000 AND 7000";
                } else if (price == 6) {
                    strCommand +=" AND tbl_RealEstate.Price BETWEEN 7000 AND 10000";
                } else if (price == 7) {
                    strCommand +=" AND tbl_RealEstate.Price BETWEEN 10000 AND 50000";
                } else if (price == 8) {
                    strCommand +=" AND tbl_RealEstate.Price BETWEEN 50000 AND 100000";
                } else if (price == 9) {
                    strCommand +=" AND tbl_RealEstate.Price BETWEEN 100000 AND 500000";
                } else if (price == 10) {
                    strCommand +=" AND tbl_RealEstate.Price BETWEEN 500000 AND 1000000";
                } else if (price == 11) {
                    strCommand +=" AND tbl_RealEstate.Price BETWEEN 1000000 AND 3000000";
                } else if (price == 12) {
                    strCommand +=" AND tbl_RealEstate.Price BETWEEN 3000000 AND 7000000";
                } else if (price == 13) {
                    strCommand +=" AND tbl_RealEstate.Price BETWEEN 7000000 AND 10000000";
                }  else if (price == 14){
                    strCommand +=" AND tbl_RealEstate.Price > 10000000";
                }
            }
            if( request.getParameter("orientareation")!=null){
                try{
                 area = Integer.parseInt(request.getParameter("orientareation"));
                strCommand +=" AND tbl_RealEstate.orientation LIKE '%"+orientation+"%'";
                }catch(NumberFormatException e){
                    System.out.println("Loi");
                }
            }
            if (deposit != 0) {
                if (deposit == 1) {
                    strCommand +=" AND tbl_RealEstate.Deposit BETWEEN 50 AND 500";
                } else if (deposit == 2) {
                    strCommand +=" AND tbl_RealEstate.Deposit BETWEEN 500 AND 1000";
                } else if (deposit == 3) {
                    strCommand +=" AND tbl_RealEstate.Deposit BETWEEN 1000 AND 2000";
                } else if (deposit == 4) {
                    strCommand +=" AND tbl_RealEstate.Deposit BETWEEN 2000 AND 3000";
                } else if (deposit == 5) {
                    strCommand +=" AND tbl_RealEstate.Deposit BETWEEN 3000 AND 7000";
                } else if (deposit == 6) {
                    strCommand +=" AND tbl_RealEstate.Deposit BETWEEN 7000 AND 10000";
                } else if (deposit == 7) {
                    strCommand +=" AND tbl_RealEstate.Deposit BETWEEN 10000 AND 50000";
                } else if (deposit == 8) {
                    strCommand +=" AND tbl_RealEstate.Deposit BETWEEN 50000 AND 100000";
                } else if (deposit == 9) {
                    strCommand +=" AND tbl_RealEstate.Deposit BETWEEN 100000 AND 500000";
                } else if (deposit == 10) {
                    strCommand +=" AND tbl_RealEstate.Deposit > 500000";
                }
            }
            if (area != 0) {
                if (area == 1) {
                    strCommand +=" AND tbl_RealEstate.area BETWEEN 10 AND 20";
                } else if (area == 2) {
                    strCommand +=" AND tbl_RealEstate.area BETWEEN 20 AND 30";
                } else if (area == 3) {
                    strCommand +=" AND tbl_RealEstate.area BETWEEN 30 AND 40";
                } else if (area == 4) {
                    strCommand +=" AND tbl_RealEstate.area BETWEEN 40 AND 50";
                } else if (area == 5) {
                    strCommand +=" AND tbl_RealEstate.area BETWEEN 50 AND 60";
                } else if (area == 6) {
                    strCommand +=" AND tbl_RealEstate.area BETWEEN 60 AND 70";
                } else if (area == 7) {
                    strCommand +=" AND tbl_RealEstate.area BETWEEN 70 AND 80";
                } else if (area == 8) {
                    strCommand +=" AND tbl_RealEstate.area BETWEEN 80 AND 90";
                } else if (area == 9) {
                    strCommand +=" AND tbl_RealEstate.area BETWEEN 90 AND 100";
                } else if (area == 10) {
                    strCommand +=" AND tbl_RealEstate.area > 100";
                }
            }
            HttpSession session = request.getSession(true);
                session.setAttribute("strSearchCommand", strCommand);
            response.sendRedirect("searchResult.jsp");
            
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapping.findForward(SUCCESS);
    }
}
